home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / lib / posix / getpwent.c < prev    next >
C/C++ Source or Header  |  1990-07-23  |  2KB  |  129 lines

  1. /* get entry from password file
  2.  *
  3.  * By Patrick van Kleef
  4.  *
  5.  * James R. Stuhlmacher  Nov. 1989
  6.  *  - Modified for POSIX conformance.
  7.  */
  8.  
  9. #include <lib.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. #include <unistd.h>
  13. #include <pwd.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16.  
  17. #define BUFFERSIZE 1024
  18. #define PWBUFSIZE 256
  19.  
  20. PRIVATE char _pw_file[] = "/etc/passwd";
  21. PRIVATE char _pwbuf[PWBUFSIZE];
  22. PRIVATE char _buffer[BUFFERSIZE];
  23. PRIVATE char *_pnt;
  24. PRIVATE char *_buf;
  25. PRIVATE int _pw = -1;
  26. PRIVATE int _bufcnt;
  27. PRIVATE struct passwd _pwd;
  28.  
  29. PRIVATE _PROTOTYPE( int getline, (void)        );
  30. PRIVATE _PROTOTYPE( void skip_period, (void)    );
  31.  
  32. PUBLIC int setpwent()
  33. {
  34.   if (_pw >= 0)
  35.     lseek(_pw, (off_t) 0, SEEK_SET);
  36.   else
  37.     _pw = open(_pw_file, O_RDONLY);
  38.  
  39.   _bufcnt = 0;
  40.   return(_pw);
  41. }
  42.  
  43.  
  44. PUBLIC void endpwent()
  45. {
  46.   if (_pw >= 0) close(_pw);
  47.  
  48.   _pw = -1;
  49.   _bufcnt = 0;
  50. }
  51.  
  52. PRIVATE int getline()
  53. {
  54.   if (_pw < 0 && setpwent() < 0) return(0);
  55.   _buf = _pwbuf;
  56.   do {
  57.     if (--_bufcnt <= 0) {
  58.         if ((_bufcnt = read(_pw, _buffer, BUFFERSIZE)) <= 0)
  59.             return(0);
  60.         else
  61.             _pnt = _buffer;
  62.     }
  63.     *_buf++ = *_pnt++;
  64.   } while (*_pnt != '\n' && _buf < _pwbuf + PWBUFSIZE - 1);
  65.   _pnt++;
  66.   _bufcnt--;
  67.   *_buf = '\0';
  68.   _buf = _pwbuf;
  69.   return(1);
  70. }
  71.  
  72. PRIVATE void skip_period()
  73. {
  74.   while (*_buf != ':') _buf++;
  75.  
  76.   *_buf++ = '\0';
  77. }
  78.  
  79. PUBLIC struct passwd *getpwent()
  80. {
  81.   if (getline() == 0) return((struct passwd *)NULL);
  82.  
  83.   _pwd.pw_name = _buf;
  84.   skip_period();
  85.   _pwd.pw_passwd = _buf;
  86.   skip_period();
  87.   _pwd.pw_uid = (uid_t) atoi(_buf);
  88.   skip_period();
  89.   _pwd.pw_gid = (gid_t) atoi(_buf);
  90.   skip_period();
  91.   _pwd.pw_gecos = _buf;
  92.   skip_period();
  93.   _pwd.pw_dir = _buf;
  94.   skip_period();
  95.   _pwd.pw_shell = _buf;
  96.  
  97.   return(&_pwd);
  98. }
  99.  
  100. PUBLIC struct passwd *getpwnam(__name)
  101. char *__name;
  102. {
  103.   struct passwd *pwd;
  104.  
  105.   setpwent();
  106.   while ((pwd = getpwent()) != 0)
  107.     if (!strcmp(pwd->pw_name, __name)) break;
  108.   endpwent();
  109.   if (pwd != 0)
  110.     return(pwd);
  111.   else
  112.     return((struct passwd *)NULL);
  113. }
  114.  
  115. PUBLIC struct passwd *getpwuid(__uid)
  116. uid_t __uid;
  117. {
  118.   struct passwd *pwd;
  119.  
  120.   setpwent();
  121.   while ((pwd = getpwent()) != 0)
  122.     if (pwd->pw_uid == __uid) break;
  123.   endpwent();
  124.   if (pwd != 0)
  125.     return(pwd);
  126.   else
  127.     return((struct passwd *)NULL);
  128. }
  129.